//
//  UIView+XM.m
//  XMNavigationController
//
//  Created by Ce Liu on 2018/6/6.
//  Copyright © 2018年 Ce Liu. All rights reserved.
//

#import "UIView+XM.h"

@implementation UIViewVirtualLayout

- (CGFloat)x {
    return self.frame.origin.x;
}

- (void)setX:(CGFloat)x {
    self.frame = CGRectMake(x, self.y, self.width, self.height);
}

- (CGFloat)y {
    return self.frame.origin.y;
}

- (void)setY:(CGFloat)y {
    self.frame = CGRectMake(self.x, y, self.width, self.height);
}

- (CGFloat)width {
    return self.frame.size.width;
}

- (void)setWidth:(CGFloat)width {
    self.frame = CGRectMake(self.x, self.y, width, self.height);
}

- (CGFloat)height {
    return self.frame.size.height;
}

- (void)setHeight:(CGFloat)height {
    self.frame = CGRectMake(self.x, self.y, self.width, height);
}

- (CGFloat)r {
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setR:(CGFloat)r {
    CGRect frame = self.frame;
    frame.origin.x = r - frame.size.width;
    self.frame = frame;
}

- (CGFloat)b {
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setB:(CGFloat)b {
    CGRect frame = self.frame;
    frame.origin.y = b - frame.size.height;
    self.frame = frame;
}

- (CGFloat)centerX {
    return (self.x + self.r) / 2.0;
}

- (void)setCenterX:(CGFloat)centerX {
    self.x = centerX - self.width / 2.0;
}

- (CGFloat)centerY {
    return (self.y + self.b) / 2.0;
}

- (void)setCenterY:(CGFloat)centerY {
    self.y = centerY - self.height / 2.0;
}


- (CGPoint)origin {
    return self.frame.origin;
}

- (void)setOrigin:(CGPoint)origin {
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGSize)size {
    return self.frame.size;
}

- (void)setSize:(CGSize)size {
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (void)setFrame:(CGRect)frame {
    _frame = frame;
}

@end

@implementation UIView (XM)

- (CGFloat)x {
    return self.frame.origin.x;
}

- (void)setX:(CGFloat)x {
    self.frame = CGRectMake(x, self.y, self.width, self.height);
}

- (CGFloat)y {
    return self.frame.origin.y;
}

- (void)setY:(CGFloat)y {
    self.frame = CGRectMake(self.x, y, self.width, self.height);
}

- (CGFloat)width {
    return self.frame.size.width;
}

- (void)setWidth:(CGFloat)width {
    self.frame = CGRectMake(self.x, self.y, width, self.height);
}

- (CGFloat)height {
    return self.frame.size.height;
}

- (void)setHeight:(CGFloat)height {
    self.frame = CGRectMake(self.x, self.y, self.width, height);
}

- (CGFloat)r {
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setR:(CGFloat)r {
    CGRect frame = self.frame;
    frame.origin.x = r - frame.size.width;
    self.frame = frame;
}

- (CGFloat)b {
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setB:(CGFloat)b {
    CGRect frame = self.frame;
    frame.origin.y = b - frame.size.height;
    self.frame = frame;
}

- (CGFloat)centerX {
    return self.center.x;
}

- (void)setCenterX:(CGFloat)centerX {
    self.center = CGPointMake(centerX, self.center.y);
}

- (CGFloat)centerY {
    return self.center.y;
}

- (void)setCenterY:(CGFloat)centerY {
    self.center = CGPointMake(self.center.x, centerY);
}

- (CGPoint)origin {
    return self.frame.origin;
}

- (void)setOrigin:(CGPoint)origin {
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGSize)size {
    return self.frame.size;
}

- (void)setSize:(CGSize)size {
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (void)setFrameFromLayout:(VirtualLayoutBlock)layoutBlock {
    UIViewVirtualLayout * layout = [[UIViewVirtualLayout alloc] init];
    layout.frame = self.frame;
    layoutBlock(layout);
    [self setFrame:layout.frame];
}

@end
